package com.hankol.material.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baselibrary.bean.download.DownloadInfo
import com.baselibrary.db.DBManager
import com.baselibrary.utils.SpUtils


/**下载中
 * @author: merbng
 * @time: 2021/10/15 10:05
 * @description:
 */
class TabDownloadingViewModel : MaterialBaseViewModel() {
    var dataListLD: MutableLiveData<List<DownloadInfo>> = MutableLiveData()
    val userIds: Int by lazy { SpUtils.getInstance().userId }

    fun getDownloadList() {
        val downloadList = DBManager.getInstance().getDownloadList(userIds,true)
        val list = mutableListOf<DownloadInfo>()
        for (info in downloadList) {
            if (info.downloadStatus.equals(DownloadInfo.DOWNLOAD) || info.downloadStatus.equals(DownloadInfo.DOWNLOAD_PAUSE) || info.downloadStatus.equals(DownloadInfo.DOWNLOAD_WAIT)) {
                list.add(info)
            }
        }
        //去重
        val map = LinkedHashMap<String, DownloadInfo>()
        for (info in list) {
            if (!map.containsKey(info.path)) {
                map.put(info.path, info);
            }
        }
        val cabList = ArrayList<DownloadInfo>(map.values)
        dataListLD.value = cabList
    }
}